import {observable, action} from 'mobx';
import { ListStore } from './listStore'
import { WeaTools } from 'ecCom';
import * as API_LIST from '../apis/fnaExpenseDepartment'
import * as PUBLIC_API from '../apis/fnaReportBase'
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 部门收支的Store
 */
export class FnaExpenseDeptStore extends ListStore{

	//高级搜索默认值
	@observable baseParams = {
        offical: "",
        officalType: -1
    };
	//tab默认值
	@observable searchParams = {
        index:0
    };

	constructor(){
		super();
		this.title = "";
		this.appendBaseParams = this.appendBaseParams.bind(this);
		this.initDatas = this.initDatas.bind(this);
		this.doSearch = this.doSearch.bind(this);
	}

	@action
    appendBaseParams(params) {
        this.baseParams = {...this.baseParams,...params};
    }

	
	initDatas(params = {}){
		this.initInfo();
		//初始化tab列表
		PUBLIC_API.queryYearsPeriods().then(data =>{
			
			 
			const { yearList } = data;
			let tabTitle=[];
			let size = 0;
			for(let obj of yearList){
				let title = {title:obj.year,key:obj.id,index:obj.index};
				tabTitle.push(title);
				if(obj.current){
					this.searchParams.index = obj.index;
					this.doSearch({fnayear:obj.year});
				}
				size++;
			}
			this.topTab = tabTitle;
			this.topTabCount = size;
			
		});


			
	}

	//初始化高级搜索
	initInfo(value,params = {}){
		API_LIST.expenseDepartmentInfo().then(data =>{
			if(data.flag == 'success'){
			    this.hasRight = 1;

				const { conditions } = data;
				this.condition = conditions;
				this.form.initFormFields(conditions);
			} else {	
				this.hasRight = 4;
			}
		});
	}

	//TABLE数据
	doSearch(params = {}) {
		const searchParamsAd = this.form.getFormParams();
		const newParams = { ...this.baseParams, ...this.searchParams, ...searchParamsAd, ...params };
		this.loading = true; 
		API_LIST.queryData(newParams).then(data =>{
			if(data.flag == 'success'){
				this.hasRight = 1;
				this.tableStore.getDatas(data.datas,1);
	            this.dataKey = data.datas; 
            } else {
                this.hasRight = 4;
            }         
		});
		this.loading = false;  
	}
}